
package com.guogee.pdb.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.guogee.pdb.bean.User;
import com.guogee.pdb.db.DBHelper.UserCollection;
import com.guogee.pdb.utils.MethodsUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class UserDAO extends ADao<User> {
 
	public UserDAO(){
		dbCollection = dbHelper.getIsmartDB().getCollection(UserCollection.COLLECTION_NAME);
	}

	@Override
	public boolean insertItem(User obj) {
		if(insertItem1(obj) != null){
			return true;
		}
		return false;
	}
	
	public String insertItem1(User obj) {
		DBObject user = new BasicDBObject();
		user.put(UserCollection.ALLOW_LOGIN, obj.isAllowLogin()?"1":"0");
		user.put(UserCollection.EMAIL, obj.getEmail());
		user.put(UserCollection.FIRST_LOGIN, obj.getFirstLoginTime());
		user.put(UserCollection.LAST_LOGIN,obj.getLastLoginTime());
		user.put(UserCollection.PHONE,obj.getMobileNo());
		user.put(UserCollection.USER_LEVEL, obj.getUserLevel());
		user.put(UserCollection.USER_NAME, obj.getUserName());
		user.put(UserCollection.USER_PWD, obj.getUserPwd());
		user.put(UserCollection.USER_TYPE, obj.getUserType());
		if(obj.getThirdUserId() != null){
			user.put(UserCollection.THIRD_USERID, obj.getThirdUserId());
		}
		
		if(obj.getRealName() != null){
			user.put(UserCollection.REAL_NAME, obj.getRealName());
		}
		boolean ret =  checkWritenResult(dbCollection.insert(user));
		if(ret){
			return user.get(UserCollection.ID).toString();
		}else{
			return null;
		}
		
	}



	@Override
	public List<User> getItemByFields(Map<String, Object> con,Map<String,Integer> orderBy) {
		List<User> result = new ArrayList<User>();
		BasicDBObject query =new BasicDBObject();
		Set<String> key = con.keySet();
		Iterator<String> it = key.iterator();
		while(it.hasNext()){
			String k = it.next();
			Object v = con.get(k);
			if (MethodsUtils.isMobileNO(v.toString())) {
				query.put(k, Long.valueOf(v.toString()));
			}
			else {
				query.put(k, v);
			}
			
		}
		DBCursor cursor = dbCollection.find(query);
		while(cursor.hasNext()){
			result.add(parstItemFromCursor(cursor));
			
		}
		return result;
	}

	@Override
	public User parstItemFromCursor(DBCursor cursor) {
		return parstDBObject(cursor.next());
	}

	@Override
	public User parstDBObject(DBObject object) {
		// TODO Auto-generated method stub
		User u = new User();
		u.setId(object.get(UserCollection.ID).toString());
		u.setUserName(object.get(UserCollection.USER_NAME).toString());
		u.setUserPwd(object.get(UserCollection.USER_PWD).toString());
		u.setEmail(object.get(UserCollection.EMAIL).toString());
		u.setMobileNo(Long.valueOf(object.get(UserCollection.PHONE).toString()));
		u.setAllowLogin(object.get(UserCollection.ALLOW_LOGIN).toString().equals("0") ? false:true);
		u.setUserLevel(((Number)object.get(UserCollection.USER_LEVEL)).intValue());
		u.setFirstLoginTime((Date)object.get(UserCollection.FIRST_LOGIN));
		u.setLastLoginTime((Date)object.get(UserCollection.LAST_LOGIN));
		int type = 0;
		if(object.get(UserCollection.USER_TYPE) != null){
			type = ((Number) object.get(UserCollection.USER_TYPE)).intValue();
		}
		u.setUserType(type);
		if(object.get(UserCollection.THIRD_USERID) != null){
			String thirdUserId = object.get(UserCollection.THIRD_USERID).toString();
			 u.setThirdUserId(thirdUserId);
		}
		if(object.get(UserCollection.REAL_NAME) != null){
			u.setRealName(object.get(UserCollection.REAL_NAME).toString());
		}
		return u;
	}
	

	

}
